package com.sskj.fabi.ui.fragment;


import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.component.DaggerUserDataComponent;
import com.sskj.fabi.presenter.QuickBuyFragmentPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 快捷买币
 */
@Route(path = RConfig.FABI_FRAGMENT_QUICK_BUY)
public class QuickBuyFragment extends BaseFragment<QuickBuyFragmentPresenter> {
    @BindView(R2.id.tvTypeTitle)
    TextView tvTypeTitle;
    @BindView(R2.id.tvUnitCNY)
    TextView tvUnitCNY;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.tvUnitUSDT)
    TextView tvUnitUSDT;
    @BindView(R2.id.tvPrice)
    TextView tvPrice;
    @BindView(R2.id.tvBuyType)
    TextView tvBuyType;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @BindView(R2.id.tvFee)
    TextView tvFee;
    @BindView(R2.id.llFee)
    LinearLayout llFee;
    private boolean isNum;
    private BottomSheetDialog submitBottomSheetDialog;
    private BottomSheetDialog payTypeBottomDialog;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment_quick_buy;
    }

    @Override
    public QuickBuyFragmentPresenter getPresenter() {
        return new QuickBuyFragmentPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        isNum = false;
        changeType();
//        ClickUtil.click(20, tvBuyType, () -> {
//            isNum = !isNum;
//            etNum.setText("");
//            changeType();
//        });
        ClickUtil.click(btSubmit, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            if (TextUtils.isEmpty(etNum.getText()) && isNum) {
                ToastUtil.showShort("请输入购买数量");
                return;
            }
            if (TextUtils.isEmpty(etNum.getText()) && !isNum) {
                ToastUtil.showShort("请输入购买金额");
                return;
            }
            mPresenter.buy(null, isNum ? "USD" : "CNY", etNum.getText().toString());

//            if (payTypeBottomDialog == null) {
//                mPresenter.buy(null, isNum ? "USDT" : "CNY", etNum.getText().toString());


//                payTypeBottomDialog = TipFabiUtil.showQuickPayType(getActivity(), str -> {
//                    payTypeBottomDialog.dismiss();
//                    mPresenter.buy(str[0], isNum ? "USDT" : "CNY", etNum.getText().toString());
//                });
//                payTypeBottomDialog.show();
//                Flowable.fromIterable(PayTypeEnum.list())
//                        .filter(payTypeEnum -> payTypeEnum != PayTypeEnum.WX)
//                        .map(payTypeEnum -> payTypeEnum.getName())
//                        .toList()
//                        .subscribe((strings, throwable) -> {
//                            payBottomDialog = BottomSheetUtil.getBottomSheet(getActivity(), "支付方式", (recyclerView, i, view) -> {
//                                payBottomDialog.dismiss();
//                                mPresenter.buy(PayTypeEnum.list().get(i + 1).getQuickPayType(), isNum ? "USDT" : "CNY", etNum.getText().toString());
//
////                                submitBottomSheetDialog = TipFabiUtil.showQuickBuy(getActivity(), PayTypeEnum.list().get(i), null, null, null, str -> {
////                                    submitBottomSheetDialog.dismiss();
////                                });
////                                submitBottomSheetDialog.show();
//                            }, strings);
//                        });

//            }
//            payTypeBottomDialog.show();
        });
    }

    @Override
    protected void initData() {
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    private void changeType() {
        tvTypeTitle.setText(isNum ? "购买数量" : "购买金额");
        tvUnitCNY.setVisibility(isNum ? View.GONE : View.VISIBLE);
        tvUnitUSDT.setVisibility(isNum ? View.VISIBLE : View.GONE);
        tvBuyType.setText(isNum ? "按金额购买" : "按数量购买");
    }

    public void buySuccess(String content) {
        ARouter.getInstance().build(RConfig.HANG_GUIDE_WEB).withBoolean(Constans.IS_QUICK_BUY, true).withString(Constans.CONTENT, content).navigation();

        etNum.setText("");
    }
}
